package com.bike.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author Administrator
 * 
 */
public class WebUtil {
	/***************************************************************************
	 * 鑾峰彇URI鐨勮矾寰�,濡傝矾寰勪负http://www.babasport.com/action/post.htm?method=add,
	 * 寰楀埌鐨勫�间负"/action/post.htm"
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestURI(HttpServletRequest request) {
		return request.getRequestURI();
	}

	/**
	 * 鑾峰彇瀹屾暣璇锋眰璺緞(鍚唴瀹硅矾寰勫強璇锋眰鍙傛暟)
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestURIWithParam(HttpServletRequest request) {
		return getRequestURI(request)
				+ (request.getQueryString() == null ? "" : "?"
						+ request.getQueryString());
	}

	/**
	 * 娣诲姞cookie
	 * 
	 * @param response
	 * @param name
	 *            cookie鐨勫悕绉�
	 * @param value
	 *            cookie鐨勫��
	 * @param maxAge
	 *            cookie瀛樻斁鐨勬椂闂�(浠ョ涓哄崟浣�,鍋囧瀛樻斁涓夊ぉ,鍗�3*24*60*60; 濡傛灉鍊间负0,cookie灏嗛殢娴忚鍣ㄥ叧闂�屾竻闄�)
	 */
	public static void addCookie(HttpServletResponse response, String name,
			String value, int maxAge) {

		Cookie cookie = new Cookie(name, value);
		if (maxAge > 0)
			cookie.setMaxAge(maxAge);
		cookie.setDomain(Constant.COOKIEDOMAIN);
		cookie.setSecure(false);
		cookie.setPath("/");
		response.addCookie(cookie);

	}

	/**
	 * 鑾峰彇cookie鐨勫��
	 * 
	 * @param request
	 * @param name
	 *            cookie鐨勫悕绉�
	 * @return
	 */
	public static String getCookieByName(HttpServletRequest request, String name) {
		// /Map<String, Cookie> cookieMap = WebUtil.readCookieMap(request);
		// if(cookieMap.containsKey(name)){
		// Cookie cookie = (Cookie)cookieMap.get(name);
		// return cookie.getValue();
		// }else{
		// return null;
		// }

		String username = null;
		Cookie thisCookie = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				thisCookie = cookies[i];
				if (thisCookie.getName().equals(name)) {
					username = thisCookie.getValue();
					if (username != null && !"".equals(username)) {
						break;
					}

				}
			}
		}
		return username;

	}

	public static String getCookieByNameDES(HttpServletRequest request,
			String name) {
		// Map<String, Cookie> cookieMap = WebUtil.readCookieMap(request);
		// if(cookieMap.containsKey(name)){
		// Cookie cookie = (Cookie)cookieMap.get(name);
		// String ff="";
		// try {
		// ff = Des.decrypt(cookie.getValue());
		// } catch (Exception e) {
		// TODO 鑷姩鐢熸垚 catch 鍧�
		// e.printStackTrace();
		// }
		// return ff;
		// }else{
		// return null;
		// }

		String username = null;
		Cookie thisCookie = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				thisCookie = cookies[i];
				if (thisCookie.getName().equals(name)) {
					username = thisCookie.getValue();
					if (username != null && !"".equals(username)) {
						break;
					}

				}
			}
		}

		if (username != null) {
			try {
				username = Des.decrypt(username);
			} catch (Exception e) {
				// TODO 鑷姩鐢熸垚 catch 鍧�
				// e.printStackTrace();
			}

		}

		return username;

	}

	protected static Map<String, Cookie> readCookieMap(
			HttpServletRequest request) {
		Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
		Cookie[] cookies = request.getCookies();
		if (null != cookies) {
			for (int i = 0; i < cookies.length; i++) {
				cookieMap.put(cookies[i].getName(), cookies[i]);
			}
		}
		return cookieMap;
	}

	/**
	 * 鍘婚櫎html浠ｇ爜
	 * 
	 * @param inputString
	 * @return
	 */
	public static String HtmltoText(String inputString) {
		String htmlStr = inputString; // 鍚玥tml鏍囩鐨勫瓧绗︿覆
		String textStr = "";
		java.util.regex.Pattern p_script;
		java.util.regex.Matcher m_script;
		java.util.regex.Pattern p_style;
		java.util.regex.Matcher m_style;
		java.util.regex.Pattern p_html;
		java.util.regex.Matcher m_html;
		java.util.regex.Pattern p_ba;
		java.util.regex.Matcher m_ba;

		try {
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 瀹氫箟script鐨勬鍒欒〃杈惧紡{鎴�<script[^>]*?>[\\s\\S]*?<\\/script>
			// }
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 瀹氫箟style鐨勬鍒欒〃杈惧紡{鎴�<style[^>]*?>[\\s\\S]*?<\\/style>
			// }
			String regEx_html = "<[^>]+>"; // 瀹氫箟HTML鏍囩鐨勬鍒欒〃杈惧紡
			String patternStr = "\\s+";

			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); // 杩囨护script鏍囩

			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); // 杩囨护style鏍囩

			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); // 杩囨护html鏍囩

			p_ba = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
			m_ba = p_ba.matcher(htmlStr);
			htmlStr = m_ba.replaceAll(""); // 杩囨护绌烘牸

			textStr = htmlStr;

		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}
		return textStr;// 杩斿洖鏂囨湰瀛楃涓�
	}
	//鏄惁鍙互琚暟鎹簱in鐨勫瓧绗︿覆 濡�  1,2,2,3
	public static boolean isCanIn(String target){
		String regEx="^[0-9]+(,[0-9]+)*$";
		Pattern pattern=Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher matcher=pattern.matcher(target);
		return matcher.matches();
	}
}
